package util

import (
	"crypto/tls"
	"io"
	"net"
	"net/http"
	"sync"
	"time"
)

func init() {
	httpclient = &http.Client{
		Timeout: 30 * time.Second, // 请求超时时间
		Transport: &http.Transport{
			MaxIdleConns:        100,              // 最大空闲连接数
			IdleConnTimeout:     90 * time.Second, // 空闲连接超时
			TLSHandshakeTimeout: 10 * time.Second, // TLS握手超时
			DialContext: (&net.Dialer{
				Timeout:   30 * time.Second, // 连接超时
				KeepAlive: 30 * time.Second, // 保持活动连接时间
			}).DialContext,
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		},
	}
}

var (
	httpClientManagerOnce   sync.Once
	httpClientManagerSingle *HttpClientManager
	httpclient              *http.Client
)

type HttpClientManager struct {
}

func NewHttpClientManager() *HttpClientManager {
	httpClientManagerOnce.Do(func() {
		httpClientManagerSingle = &HttpClientManager{}
	})
	return httpClientManagerSingle
}

func (HttpClientManager *HttpClientManager) Get(url string) (resp *http.Response, err error) {
	return httpclient.Get(url)
}

func (HttpClientManager *HttpClientManager) Post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
	return httpclient.Post(url, contentType, body)
}
